package com.yunshipei.ui.activity;

import android.content.Intent;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.yunshipei.base.BaseActivity;
import com.yunshipei.common.wedigt.YspTitleBar;
import com.yunshipei.db.DatabaseUtil;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.DateString;
import com.yunshipei.model.DelayEventModel;
import com.yunshipei.model.ForwardTimeModel;
import com.yunshipei.model.NotifiContextModel;
import com.yunshipei.model.NotifyInfo;
import com.yunshipei.model.RepetitionStatusModel;
import com.yunshipei.ui.view.wheelview.OnWheelScrollListener;
import com.yunshipei.ui.view.wheelview.WheelView;
import com.yunshipei.ui.view.wheelview.adapter.NumericWheelAdapter;
import com.yunshipei.utils.AlarmSet;
import com.yunshipei.utils.FileUriUtils;
import com.yunshipei.utils.FileUtils;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

//import de.greenrobot.event.EventBus;
import io.rong.eventbus.EventBus;
import io.rong.app.utils.StringUtilSub;

public class NotifiDetailActivity extends BaseActivity implements View.OnClickListener {
    public static final String FORM = "form";
    private static final int RINGTONE_PICKED = 1;
    private LayoutInflater inflater = null;
    private WheelView min;
    private WheelView sec;
    private RelativeLayout timeLayout;
    private View view = null;
    private LinearLayout timeSet;           //时间设置
    private LinearLayout notifiEvent;       //事件类型
    private LinearLayout shake;             //震动设置
    private LinearLayout music;             //铃声设置
    private LinearLayout voiceReport;      //语音播报
    private LinearLayout repetitionTime;    //重复次数
    private LinearLayout delaySet;          //延迟设置
    //    private LinearLayout afterNotifi;   //提前提醒
    private LinearLayout newInputNotifiInfo;//提醒内容输入
    private CheckBox shakeCheck, voiceCheck, musicCheck;
    private LinearLayout delete_notifi;
    private LinearLayout CheckTag1;
    private LinearLayout CheckTag2;
    private LinearLayout CheckTag3;
    private Intent intent = null;
    private boolean status = false;
    private Bundle bundle = null;
    private NotifyInfo notifiInfo;
    private DatabaseUtil dbu = null;
    private TextView dateView = null;
    private TextView delayText = null;
    private TextView repetitionText = null;
    private String mCustomRingtone = null;
    private TextView notifiContext = null;
    private EditText editContext = null;
    private YspTitleBar mTitleBar;
    private TextView musicName = null;
    private int oldAlarmTime = 0;
    private ScrollView scrollView = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_notifi_detail);
        dbu = new DatabaseUtil();
        initView();
        initData();
        if (!status) {
            min.setCurrentItem(notifiInfo.getSpeechRecognition().getDateTime().getInfactDate().getHours() - 1);
            sec.setCurrentItem(notifiInfo.getSpeechRecognition().getDateTime().getInfactDate().getMinutes());
        }
    }

    public void initView() {
        initTitleBar();
        intent = getIntent();
        notifiInfo = (NotifyInfo) intent.getSerializableExtra("notifiInfo");
        status = intent.getBooleanExtra(NotifiDetailActivity.FORM, true);
        if (status) {
            notifiInfo = new NotifyInfo();
        } else {
            oldAlarmTime = (int) (notifiInfo.get_id() + notifiInfo.countForwardTime());
        }
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        timeLayout = (RelativeLayout) findViewById(R.id.timePicker);
        timeLayout.addView(getDataPick());
        timeSet = (LinearLayout) findViewById(R.id.time_set);
        //创建时的布局
        newInputNotifiInfo = (LinearLayout) findViewById(R.id.new_input_notifi_info);
        //编辑时的布局
        notifiEvent = (LinearLayout) findViewById(R.id.notifi_event);
        shake = (LinearLayout) findViewById(R.id.shake);
        music = (LinearLayout) findViewById(R.id.music);
        voiceReport = (LinearLayout) findViewById(R.id.voice_report);
        repetitionTime = (LinearLayout) findViewById(R.id.repetition_time);
        delaySet = (LinearLayout) findViewById(R.id.delay_set);
//        afterNotifi = (LinearLayout) findViewById(R.id.after_notifi);
        delete_notifi = (LinearLayout) findViewById(R.id.delete_notifi);
        shakeCheck = (CheckBox) findViewById(R.id.shake_check);
        voiceCheck = (CheckBox) findViewById(R.id.voice_report_check);
        musicCheck = (CheckBox) findViewById(R.id.music_check);
        dateView = (TextView) findViewById(R.id.date_str);
        repetitionText = (TextView) findViewById(R.id.repetition_text);
        delayText = (TextView) findViewById(R.id.delay_text);
        notifiContext = (TextView) findViewById(R.id.notifi_context);
        editContext = (EditText) findViewById(R.id.edit_notifi_context);
        dateView.setText(notifiInfo.getSpeechRecognition().getDateTime().getDate());
        notifiContext.setText(notifiInfo.getSpeechRecognition().getContent());
        repetitionText.setText(notifiInfo.getRepetitionTime());
        scrollView = (ScrollView) findViewById(R.id.scroll);
        musicName = (TextView) findViewById(R.id.music_name);
        delayText.setText(notifiInfo.getDelayTime() + " 分钟");
        timeSet.setOnClickListener(this);
        newInputNotifiInfo.setOnClickListener(this);
        shake.setOnClickListener(this);
        music.setOnClickListener(this);
        voiceReport.setOnClickListener(this);
        repetitionTime.setOnClickListener(this);
        delaySet.setOnClickListener(this);
//        afterNotifi.setOnClickListener(this);
        notifiEvent.setOnClickListener(this);
        delete_notifi.setOnClickListener(this);
        //显示不同布局
        showOrGoneTitle(status);
        //保存原有提醒时间
    }

    private void initTitleBar() {
        mTitleBar = (YspTitleBar) findViewById(R.id.title_bar);
        mTitleBar.setTitle("提醒");
        mTitleBar.setSubTitle("保存");
        mTitleBar.setSubTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (status) {
                    /**
                     * 设置新闹钟
                     * do something
                     */
                    notifiInfo.getSpeechRecognition().getDateTime().setTime((min.getCurrentItem() + 1) + ":" + sec.getCurrentItem() + ":00");
                    Log.d("ma",(min.getCurrentItem() + 1) + ":" + sec.getCurrentItem() + ":00");
                    notifiInfo.getSpeechRecognition().getDateTime().setTimeOrig(computeTime(min.getCurrentItem() + 1) + sec.getCurrentItem() + "分");
                    Log.d("ma",computeTime(min.getCurrentItem() + 1) + sec.getCurrentItem() + "分");
                    if (checkNotifiTime(notifiInfo) && getEditContext(notifiInfo)) {
                        int id = 214740000;
                        id = dbu.findMaxValueforId();
                        dbu.insertAlarm(notifiInfo);
                        setAlarm(notifiInfo, id + 1);
                        EventBus.getDefault().post(notifiInfo);
                        finish();
                        /*showToast("提醒设置成功");*/
                    }
                } else if (checkNotifiTime(notifiInfo)) {
                    //关闭闹钟
                    cancelAlarm(notifiInfo, (int) oldAlarmTime);
                    //更新数据库
                    notifiInfo.setNotifiStatus("true");
                    dbu.updateNotifi(notifiInfo);
                    setAlarm(notifiInfo, notifiInfo.get_id());
                    EventBus.getDefault().post(notifiInfo);
                    finish();
                    showToast("提醒更新成功");
                }
            }
        });
    }

    private void initData() {
        if (notifiInfo.getShake()) {
            shakeCheck.setChecked(Boolean.valueOf(notifiInfo.getShake()));
        } else {
            shakeCheck.setChecked(false);
        }
        if (!status) {
            notifiContext.setText(notifiInfo.getSpeechRecognition().getContent());
        }
        if (Boolean.valueOf(notifiInfo.getVoiceRepore())) {
            voiceCheck.setChecked(Boolean.valueOf(notifiInfo.getVoiceRepore()));
        } else {
            voiceCheck.setChecked(false);
        }
        if (notifiInfo.getMusicStatus()) {
            musicCheck.setChecked(notifiInfo.getMusicStatus());
            music.setVisibility(View.VISIBLE);
        } else {
            musicCheck.setChecked(false);
            music.setVisibility(View.GONE);
        }

        editContext.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        if (editContext.getParent() != null) {
                            editContext.getParent().requestDisallowInterceptTouchEvent(true);
                        }
                        break;
                }
                return false;
            }
        });
        shakeCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //shakeChack
                notifiInfo.setShake(isChecked);
            }
        });

        voiceCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //voiceCheck
                notifiInfo.setVoiceRepore(isChecked);
            }
        });
        musicCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //musicCheck
                notifiInfo.setMusicStatus(isChecked);
                if (isChecked) {
                    music.setVisibility(View.VISIBLE);
                } else {
                    music.setVisibility(View.GONE);
                }
            }
        });
    }


    private View getDataPick() {
        Calendar c = Calendar.getInstance();
        //获取时间滚轮
        view = inflater.inflate(R.layout.wheel_date_picker, null);
        min = (WheelView) view.findViewById(R.id.min);
        NumericWheelAdapter numericWheelAdapter3 = new NumericWheelAdapter(this, 1, 23, "%02d");
        numericWheelAdapter3.setLabel("时");
        min.setViewAdapter(numericWheelAdapter3);
        min.setCyclic(true);
        min.addScrollingListener(scrollListener);

        sec = (WheelView) view.findViewById(R.id.sec);

        NumericWheelAdapter numericWheelAdapter4 = new NumericWheelAdapter(this, 0, 59, "%02d");
        numericWheelAdapter4.setLabel("分");
        sec.setViewAdapter(numericWheelAdapter4);

        sec.setCyclic(true);
        sec.addScrollingListener(scrollListener);
        min.setVisibleItems(7);
        sec.setVisibleItems(7);
        if (status) {
            min.setCurrentItem(c.getTime().getHours() - 1);
            sec.setCurrentItem(c.getTime().getMinutes());
        } else {
            min.setCurrentItem(notifiInfo.getSpeechRecognition().getDateTime().getInfactDate().getHours() - 1);
            sec.setCurrentItem(notifiInfo.getSpeechRecognition().getDateTime().getInfactDate().getMinutes());
        }
        return view;
    }

    OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {
        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            notifiInfo.getSpeechRecognition().getDateTime().setTime((min.getCurrentItem() + 1) + ":" + sec.getCurrentItem() + ":00");
            notifiInfo.getSpeechRecognition().getDateTime().setTimeOrig(computeTime(min.getCurrentItem() + 1) + sec.getCurrentItem() + "分");
        }
    };

    public String computeTime(int hour) {
        if (hour <= 14) {
            if (hour >= 10) {
                return "中午" + (hour) + "点";
            } else {
                return "早上" + (hour) + "点";
            }

        } else if (hour <= 18) {
            return "下午" + (hour - 12) + "点";
        } else {
            return "晚上" + (hour - 12) + "点";
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.time_set:
                Uri uri = Uri.parse("Enterplorer://" + getApplicationInfo().packageName).buildUpon()
                        .appendPath("DateTime").appendQueryParameter("target", "time").build();
                if (status) {
                    notifiInfo = new NotifyInfo();
                }
                Date n = notifiInfo.getSpeechRecognition().getDateTime().getInfactDate();
                sendIntent(uri, notifiInfo);
                break;
            case R.id.new_input_notifi_info:
                break;
            case R.id.shake:
//                changeShake(notifiInfo);
                break;
            case R.id.music:
//                通知铃声
                doPickRingtone();
                break;
            case R.id.voice_report:
//                语音播报
//                if (Boolean.valueOf(notifiInfo.getVoiceRepore())) {
//                    notifiInfo.setVoiceRepore("true");
//                } else {
//                    notifiInfo.setVoiceRepore("false");
//                }
                break;
            case R.id.repetition_time:
                Uri uri2 = Uri.parse("Enterplorer://" + getApplicationInfo().packageName).buildUpon()
                        .appendPath("RepetitionTime").appendQueryParameter("target", "repetition").build();
                sendIntent(uri2);
                break;
            case R.id.delay_set:
                Uri uri3 = Uri.parse("Enterplorer://" + getApplicationInfo().packageName).buildUpon()
                        .appendPath("DelaySet").appendQueryParameter("target", "repetition").build();
                sendIntent(uri3, notifiInfo);
                break;
            case R.id.after_notifi:
                //提前提醒
                Uri uri1 = Uri.parse("Enterplorer://" + getApplicationInfo().packageName).buildUpon()
                        .appendPath("ForwardTime").appendQueryParameter("target", "afterTime").build();
                sendIntent(uri1);
                break;
        /*    case R.id.save:
                *//**
                 * do something about save info for notification
                 *//*
                if (status) {
                    *//**
                     * 设置新闹钟
                     * do something
                     *//*
                    if (checkNotifiTime(notifiInfo) && getEditContext(notifiInfo)) {
                        int id = 214740000;
                        id = dbu.findMaxValueforId();
                        dbu.insertAlarm(notifiInfo);
                        setAlarm(notifiInfo, id + 1);
                        EventBus.getDefault().post(notifiInfo);
                        finish();
                *//*showToast("提醒设置成功");*//*
                    }
                } else {
                    //关闭闹钟
                    cancelAlarm(notifiInfo, oldAlarmTime);
                    //更新数据库
                    dbu.updateNotifi(notifiInfo);
                    setAlarm(notifiInfo, notifiInfo.get_id());
                    EventBus.getDefault().post(notifiInfo);
                    finish();
                    showToast("提醒更新成功");
                }
                break;*/
            case R.id.notifi_event:
                Uri uri5 = Uri.parse("Enterplorer://" + getApplicationInfo().packageName).buildUpon()
                        .appendPath("Context").appendQueryParameter("target", "context").build();
                sendIntent(uri5);
                break;
            case R.id.delete_notifi:
                //删除提醒
                dbu.deleteNotifi(notifiInfo.get_id());
                AlarmSet as = new AlarmSet(NotifiDetailActivity.this);
                as.cancelAlarm(notifiInfo);
                Toast.makeText(NotifiDetailActivity.this, "提醒删除成功", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(notifiInfo);
                finish();
                break;
            default:
                break;
        }
    }

    public void showOrGoneTitle(boolean bool) {
        if (bool) {
            //新建
            newInputNotifiInfo.setVisibility(View.VISIBLE);
            notifiEvent.setVisibility(View.GONE);
            delete_notifi.setVisibility(View.GONE);
            mTitleBar.setTitle("新建提醒");
        } else {
            //编辑
            newInputNotifiInfo.setVisibility(View.GONE);
            notifiEvent.setVisibility(View.VISIBLE);
            delete_notifi.setVisibility(View.VISIBLE);
            mTitleBar.setTitle("编辑提醒");
        }
    }

    public void changeShake(NotifyInfo notifiInfo) {
        if (notifiInfo.getShake()) {
            dbu.updateNotifiShake(notifiInfo.get_id(), "false");
            notifiInfo.setShake(false);
//            checkTag1.setBackgroundColor(Color.parseColor("#fefefe"));
            /**
             * do somethings
             */
        } else {
            dbu.updateNotifiShake(notifiInfo.get_id(), "true");
            notifiInfo.setShake(true);
//            checkTag1.setBackgroundColor(Color.parseColor("#ED4832"));
            /**
             * do somethings
             */
        }
    }

    /**
     * 获取提醒内容
     *
     * @param notifiInfo
     * @return
     */

    public boolean getEditContext(NotifyInfo notifiInfo) {
        String editNotifiContext = editContext.getText().toString();
        if (!TextUtils.isEmpty(StringUtilSub.replaceBlank(editNotifiContext))) {
            notifiInfo.getSpeechRecognition().setContent(editNotifiContext);
            return true;
        }
        showToast("提醒内容不能为空");
        return false;
    }

    /**
     * 判断提醒时间先后
     *
     * @param notifiInfo
     * @return
     */
    public boolean checkNotifiTime(NotifyInfo notifiInfo) {
        if (notifiInfo.getSpeechRecognition().getDateTime().getInfactDate().after(Calendar.getInstance().getTime())) {
            return true;
        } else {
            showToast("提醒时间不正确");
        }
        return false;
    }

    public void sendIntent(Uri uri) {
        sendIntent(uri, null);
    }

    public void sendIntent(Uri uri, NotifyInfo notifiInfo) {
        Intent intent1 = new Intent();
        intent1.setData(uri);
        intent1.putExtra("notifi", notifiInfo);
        intent1.setClass(NotifiDetailActivity.this, AlarmSetActivity.class);
        startActivity(intent1);
    }

    /**
     * time set deal
     *
     * @param ds
     */

    public void onEventMainThread(DateString ds) {
        dateView.setText(ds.getDateString());
        notifiInfo.getSpeechRecognition().getDateTime().setDate(ds.getDateString());
    }

    public void onEventMainThread(NotifiContextModel notifiContextModel) {
        notifiContext.setText(notifiContextModel.getContext());
        notifiInfo.getSpeechRecognition().setContent(notifiContextModel.getContext());
    }

    /**
     * repetition set deal
     *
     * @param repetitionStatusModel
     */
    public void onEventMainThread(RepetitionStatusModel repetitionStatusModel) {
        if (repetitionStatusModel.getRepetitionStatusArray()[7] == true) {
            //每天
            repetitionText.setText("每天");
            notifiInfo.setRepetitionTime("每天");
        } else if (repetitionStatusModel.getRepetitionStatusArray()[8] == true) {
            //从不
            repetitionText.setText("从不");
            notifiInfo.setRepetitionTime("");
        } else {
            //处理1~7
            repetitionText.setText(timeString(repetitionStatusModel.getRepetitionStatusArray()));
            notifiInfo.setRepetitionTime(timeString(repetitionStatusModel.getRepetitionStatusArray()));
        }

    }


    private void doPickRingtone() {
        //打开系统铃声设置
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false);
        // Show only ringtones设置类型为来电
//        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
        Uri ringtoneUri;
        if (mCustomRingtone != null) {
            ringtoneUri = Uri.parse(mCustomRingtone);
        } else {
            ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        }
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtoneUri);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "提醒铃声设置");
        startActivityForResult(intent, RINGTONE_PICKED);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case RINGTONE_PICKED:
                // 选择完铃声之后获得选中铃音的URI,将其值存入数据库
                Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                String fileName = FileUriUtils.getFilePathFromUri(this, pickedUri);
                notifiInfo.setMusicName(FileUtils.getFileName(fileName));
                notifiInfo.setMusic(changeUri(pickedUri) != null ? changeUri(pickedUri).toString() : "");
                musicName.setText(notifiInfo.getMusicName());
                break;
            default:
                break;
        }
    }

    public Uri changeUri(Uri uri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor actualimagecursor = this.getContentResolver().query(uri, proj, null, null, null);
        if (actualimagecursor == null)
            return null;
        int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualimagecursor.moveToFirst();


        String img_path = actualimagecursor.getString(actual_image_column_index);
        File file = new File(img_path);
        Uri fileUri = Uri.fromFile(file);
        return fileUri;
    }

    /**
     * delay deal
     *
     * @param delayEventModel
     */
    public void onEventMainThread(DelayEventModel delayEventModel) {
        delayText.setText(delayEventModel.getDelayTime() + "分钟");
        notifiInfo.setDelayTime(String.valueOf(delayEventModel.getDelayTime()));
        /**
         * do something about save
         */
    }

    public void onEventMainThread(ForwardTimeModel forwardTimeModel) {
        if (forwardTimeModel != null) {
            if (forwardTimeModel.getUnit().equals("分钟")) {
                notifiInfo.setForwardTime(String.valueOf(forwardTimeModel.getValue()));
            } else if (forwardTimeModel.getUnit().equals("小时")) {
                notifiInfo.setForwardTime(String.valueOf(forwardTimeModel.getValue() * 60));
            } else if (forwardTimeModel.getUnit().equals("天")) {
                notifiInfo.setForwardTime(String.valueOf(forwardTimeModel.getValue() * 60 * 24));
            }
        }
    }

    /**
     * 设置一次性提醒
     *
     * @param alarmTime
     */

    /**
     * 设置一次性提醒
     *
     * @param ni        提醒内容
     * @param alarmTime 提醒时间
     */
    public void setAlarm(NotifyInfo ni, long alarmTime) {
        AlarmSet alarmSet = new AlarmSet(NotifiDetailActivity.this);
        alarmSet.setAlarm(ni, alarmTime);
    }

    public void setAlarm(NotifyInfo ni, long alarmTime, int id) {
        AlarmSet alarmSet = new AlarmSet(NotifiDetailActivity.this);
        alarmSet.setAlarm(ni, alarmTime, id);
    }

    /**
     * 更新通知
     *
     * @param notifiInfo
     */
    public void setAlarm(NotifyInfo notifiInfo) {
        String repetitionTime = notifiInfo.getRepetitionTime();
        AlarmSet alarmSet = new AlarmSet(NotifiDetailActivity.this);
        if (repetitionTime.equals("每天")) {
            alarmSet.setAlarm(notifiInfo, 24 * 60 * 60 * 1000, notifiInfo.countForwardTime());
        } else if (repetitionTime.equals("从不")) {
            alarmSet.setAlarm(notifiInfo, notifiInfo.countForwardTime());
        } else if (!repetitionTime.equals("")) {
            for (int i = 0; i < 7; i++) {
                if (notifiInfo.getRepetitionBool()[i]) {
                    alarmSet.setAlarm(notifiInfo, 7 * 24 * 60 * 60 * 1000, notifiInfo.countForwardTime());
                }
            }
        } else {

        }
    }

    /**
     * 新建循环提醒
     *
     * @param notifiInfo
     * @param id
     */
    public void setAlarm(NotifyInfo notifiInfo, int id) {
        String repetitionTime = notifiInfo.getRepetitionTime();
        AlarmSet alarmSet = new AlarmSet(NotifiDetailActivity.this);
        if (repetitionTime.equals("每天")) {
            //设置每天重复提醒
            alarmSet.setAlarm(notifiInfo, 24 * 60 * 60 * 1000, notifiInfo.countForwardTime(), id);
        } else if (repetitionTime.equals("从不")) {
            //设置一次提醒
            alarmSet.setAlarm(notifiInfo, notifiInfo.countForwardTime(), id);
        } else if (!repetitionTime.equals("")) {
            for (int i = 0; i < 7; i++) {
                if (notifiInfo.getRepetitionBool()[i]) {
                    alarmSet.setAlarm(notifiInfo, 7 * 24 * 60 * 60 * 1000, notifiInfo.countForwardTime(), id);
                }
            }
        } else {
            alarmSet.setAlarm(notifiInfo, notifiInfo.countForwardTime(), id);
        }
    }

    public void cancelAlarm(NotifyInfo notifiInfo, int id) {
        AlarmSet alarmSet = new AlarmSet(NotifiDetailActivity.this);
        alarmSet.cancelAlarm(notifiInfo, id);
    }

    public String timeString(boolean[] bool) {
        //重复提醒状态
        notifiInfo.setRepetitionBool(bool);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("周");
        for (int i = 0; i < 7; i++) {
            if (bool[i]) {
                stringBuffer.append(transitionNumChina(i) + ",");
            }
        }
        int strLength = stringBuffer.length();
        if (strLength > 1) {
            return stringBuffer.substring(0, strLength - 1).toString();
        }
        return "";
    }

    private String transitionNumChina(int index) {
        HashMap<Integer, String> map = getMap();
        if (index < 7) {
            if (map != null) {
                return map.get(index);
            }
        }
        return "";
    }

    private HashMap<Integer, String> getMap() {
        HashMap<Integer, String> hashMap = new HashMap<>();
        hashMap.put(0, "一");
        hashMap.put(1, "二");
        hashMap.put(2, "三");
        hashMap.put(3, "四");
        hashMap.put(4, "五");
        hashMap.put(5, "六");
        hashMap.put(6, "日");
        return hashMap;
    }

    public void showToast(String str) {
        Toast.makeText(NotifiDetailActivity.this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
